﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmViTriLuuTru : DevExpress.XtraEditors.XtraForm
    {
        #region Khai báo biến
        ViTriLuuTru_BLL vt_bll = new ViTriLuuTru_BLL();
        DataTable dt = new DataTable();
        TextMessageBox box = new TextMessageBox();
        public string maViTri = "";
        private bool isOk = false;
        #endregion

        #region LoadForm
        public frmViTriLuuTru()
        {
            InitializeComponent();
        }

        private void frmViTriLuuTru_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        #endregion

        #region Các chức năng

        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = true;
            UpdateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = false;
            UpdateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Setinterface();
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        #endregion

        #region các sự kiện
        private void frmViTriLuuTru_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }
        #endregion

        #region Hàm viết thêm

        private void UpdateData()
        {
            if (checkValid())
            {
                ViTriLuuTru_Public vt = new ViTriLuuTru_Public();
                if (maViTri == "")
                {
                    if (checkTenInsert())
                    {
                        vt.MaViTri = box.AutoID("VT", 2, vt_bll.LayDuLieu());
                        vt.TenViTri = txtTenViTri.Text;
                        vt.MoTa = txtMoTa.Text;

                        vt_bll.Insert(vt, vt, vt);
                        CloseForm();
                    }
                }
                else
                {
                    vt.MaViTri = txtMaViTri.Text.Trim();
                    vt.TenViTri = txtTenViTri.Text;
                    vt.MoTa = txtMoTa.Text;

                    vt_bll.Update(vt, vt, vt);
                    CloseForm();
                }
            }
        }

        private void CloseForm()
        {
            if (isOk == true)
                DialogResult = DialogResult.OK;
            else
                Setinterface();
        }
        private void LoadData()
        {
            if(maViTri!="")
            {
                dt = vt_bll.TaoBang(box.sqlCheckExist("DM_ViTriLuuTru","MaViTri",maViTri));
                DataRow dr=dt.Rows[0];
                txtMaViTri.Text = maViTri;
                txtTenViTri.Text=dr["TenViTri"].ToString();
                txtMoTa.Text = dr["MoTa"].ToString();
            }
        }
        private void Setinterface()
        {
            txtMaViTri.Text = "Mã tự sinh";
            txtMoTa.Text = "";
            txtTenViTri.Text = "";
            txtTenViTri.Focus();
            maViTri = "";
        }
        private bool checkValid()
        {

            if (txtTenViTri.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên vị trí.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtTenViTri.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenInsert()
        {
            dt = vt_bll.TaoBang(box.sqlCheckExist("DM_ViTriLuuTru","TenViTri",txtTenViTri.Text.Trim()));
            if(dt.Rows.Count>0)
            {
                XtraMessageBox.Show(box.Exist(txtTenViTri.Text),box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                txtTenViTri.Focus();
                return false;
            }
            return true;
        }
        #endregion
    }
}